package com.r7data.ht.util;

import java.nio.charset.Charset;
import java.util.Arrays;

/**
 * java 基础类型与 byte[] 相互转换
 * @author ysq
 *
 */
public class ByteUtil {
	
	/**
	 * short 转 byte[]
	 * @param data
	 * @return
	 */
	public static byte[] getShortBytes(short data) {
		byte[] bytes = new byte[2];
		bytes[0] = (byte) (data & 0xff);
		bytes[1] = (byte) ((data & 0xff00) >> 8);
		return bytes;
	}

	/**
	 * chart 转 byte[]
	 * @param data
	 * @return
	 */
	public static byte[] getCharacterBytes(char data) {
		byte[] bytes = new byte[2];
		bytes[0] = (byte) (data);
		bytes[1] = (byte) (data >> 8);
		return bytes;
	}

	/**
	 * int 转 byte[]
	 * @param data
	 * @return
	 */
	public static byte[] getIntegerBytes(int data) {
		byte[] bytes = new byte[4];
		bytes[0] = (byte) (data & 0xff);
		bytes[1] = (byte) ((data & 0xff00) >> 8);
		bytes[2] = (byte) ((data & 0xff0000) >> 16);
		bytes[3] = (byte) ((data & 0xff000000) >> 24);
		return bytes;
	}

	/**
	 * long 转 byte[]
	 * @param data
	 * @return
	 */
	public static byte[] getLongBytes(long data) {
		byte[] bytes = new byte[8];
		bytes[0] = (byte) (data & 0xff);
		bytes[1] = (byte) ((data >> 8) & 0xff);
		bytes[2] = (byte) ((data >> 16) & 0xff);
		bytes[3] = (byte) ((data >> 24) & 0xff);
		bytes[4] = (byte) ((data >> 32) & 0xff);
		bytes[5] = (byte) ((data >> 40) & 0xff);
		bytes[6] = (byte) ((data >> 48) & 0xff);
		bytes[7] = (byte) ((data >> 56) & 0xff);
		return bytes;
	}

	/**
	 * float 转 byte[]
	 * @param data
	 * @return
	 */
	public static byte[] getFloatBytes(float data) {
		int intBits = Float.floatToIntBits(data);
		
		byte[] b = new byte[4];
		
		for(int i = 0;i < 4;i++){
			b[i] = (byte)(intBits >> (24 - i * 8));
		}
		return b;
	}

	/**
	 * double 转 byte[]
	 * @param data
	 * @return
	 */
	public static byte[] getDoubleBytes(double data) {
		long intBits = Double.doubleToLongBits(data);
		return getLongBytes(intBits);
	}

	/**
	 * String 转 byte[]
	 * @param data
	 * @param charsetName
	 * @return
	 */
	public static byte[] getStringBytes(String data, String charsetName) {
		Charset charset = Charset.forName(charsetName);
		return data.getBytes(charset);
	}

	/**
	 * String 转 byte[]
	 * @param data
	 * @return
	 */
	public static byte[] getStringBytes(String data) {
		return data.getBytes();
	}

	/**
	 * byte[] 转short 
	 * @param bytes
	 * @return
	 */
	public static short getShort(byte[] bytes) {
		return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
	}

	/**
	 * byte[] 转 char
	 * @param bytes
	 * @return
	 */
	public static char getChar(byte[] bytes) {
		return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
	}

	/**
	 * byte[] 转 int
	 * @param bytes
	 * @return
	 */
	public static int getInt(byte[] bytes) {
		return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16))
				| (0xff000000 & (bytes[3] << 24));
	}

	/**
	 * byte[] 转 long
	 * @param bytes
	 * @return
	 */
	public static long getLong(byte[] bytes) {
		return (0xffL & (long) bytes[0]) | (0xff00L & ((long) bytes[1] << 8)) | (0xff0000L & ((long) bytes[2] << 16))
				| (0xff000000L & ((long) bytes[3] << 24)) | (0xff00000000L & ((long) bytes[4] << 32))
				| (0xff0000000000L & ((long) bytes[5] << 40)) | (0xff000000000000L & ((long) bytes[6] << 48))
				| (0xff00000000000000L & ((long) bytes[7] << 56));
	}

	/**
	 * byte[] 转 float
	 * @param bytes
	 * @return
	 */
	public static float getFloat(byte[] b) {
		int l;
		l = b[0];
		l &= 0xff;
		l |= ((long) b[1] << 8);
		l &= 0xffff;
		l |= ((long) b[2] << 16);
		l &= 0xffffff;
		l |= ((long) b[3] << 24);
		
		return Float.intBitsToFloat(l);
	}

	/**
	 * byte[] 转 double
	 * @param bytes
	 * @return
	 */
	public static double getDouble(byte[] bytes) {
		long l = getLong(bytes);
		return Double.longBitsToDouble(l);
	}

	/**
	 * byte[] 转 String
	 * @param bytes
	 * @param charsetName
	 * @return
	 */
	public static String getString(byte[] bytes, String charsetName) {
		return new String(bytes, Charset.forName(charsetName));
	}

	/**
	 * byte[] 转 String
	 * @param bytes
	 * @return
	 */
	public static String getString(byte[] bytes) {
		return new String(bytes);
	}
	
	/**
	 * 追加数组
	 * @param target
	 * @param append
	 * @return
	 */
	public static byte[] appendByte(byte[] target, byte[] append){
		int originalLength = target.length;
		int appendLength = append.length;
		//先扩容长度
		int totalLength = originalLength + appendLength;
		
		target = Arrays.copyOf(target, totalLength);
		
		System.arraycopy(append, 0, target, originalLength, appendLength);
		
		return target;
	}

	public static void main(String[] args) {
		
		short s = 1111;
		int i = 2222;
		long l = 333333;
		char c = 'c';
		float f = 444.44f;
		double d = 555.55;
		String string = "测试字符串666";
		
		System.out.println(s);
		System.out.println(i);
		System.out.println(l);
		System.out.println(c);
		System.out.println(f);
		System.out.println(d);
		System.out.println(string);

		System.out.println("**************");

		System.out.println(getShort(getShortBytes(s)));
		System.out.println(getInt(getIntegerBytes(i)));
		System.out.println(getLong(getLongBytes(l)));
		System.out.println(getChar(getCharacterBytes(c)));
		System.out.println(getFloat(getFloatBytes(f)));
		System.out.println(getDouble(getDoubleBytes(d)));
		System.out.println(getString(getStringBytes(string)));
	}
}
